package adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianfeng.lingshijia.R;

import java.util.List;

import entity.GoodsEntity;
import utils.DBUtils;
import utils.DownloadUtils;
import utils.ShoppingCart;

public class OtherGoodsSingleAdapter extends BaseAdapter {

    private List<GoodsEntity> list;
    private Context context;
    private LayoutInflater layoutInflater;
    private String marketName;

    /**
     * 构造方法
     *
     * @param context 上下文对象
     * @param list    存储货物的对象
     */
    public OtherGoodsSingleAdapter(Context context, List<GoodsEntity> list,String marketName) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.list = list;
        this.marketName = marketName;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public GoodsEntity getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean isEnabled(int position) {
        return list.get(position).getType() != 1;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //如果控件不为空并且控件的类型和Item的类型一致，控件复用
        if (convertView != null && ((ViewHolder) convertView.getTag()).type == getItem(position).getType()) {
            setDataToView(position, convertView);
        } else {//否则重新创建控件
            final ViewHolder viewHolder = new ViewHolder();
            switch (getItem(position).getType()) {
                case 0://type为0时需要找到的控件，标题类控件
                    convertView = layoutInflater.inflate(R.layout.school_area_layout, null);
                    convertView.setClickable(false);//设置为不可点击
                    viewHolder.tvSchoolAreaName = (TextView) convertView.findViewById(R.id.tvSchoolAreaName);
                    break;
                case 1://type为1时需要找到的控件，组合特卖控件
                    convertView = layoutInflater.inflate(R.layout.combination_sell_single_layout, null);
                    convertView.setClickable(false);//设置为不可点击
                    viewHolder.type = 1;//设置控件类型
                    viewHolder.ivCombinationGoods1 = (ImageView) convertView.findViewById(R.id.ivCombinationGoods1);//商品图1
                    viewHolder.ivCombinationGoods2 = (ImageView) convertView.findViewById(R.id.ivCombinationGoods2);//商品图2
                    viewHolder.tvCombinationPrice = (TextView) convertView.findViewById(R.id.tvCombinationPrice);//价格
                    viewHolder.tvCombinationGoods1 = (TextView) convertView.findViewById(R.id.tvCombinationGoods1);//商品名称1
                    viewHolder.tvCombinationGoods2 = (TextView) convertView.findViewById(R.id.tvCombinationGoods2);//商品名称2

                    viewHolder.ivReduceCombinationGoods = (ImageView) convertView.findViewById(R.id.ivReduceCombinationGoods);//－
                    viewHolder.tvCombinationGoodsNumber = (TextView) convertView.findViewById(R.id.tvCombinationGoodsNumber);//购买数量
                    viewHolder.ivAddCombinationGoods = (ImageView) convertView.findViewById(R.id.ivAddCombinationGoods);//+
                    viewHolder.tvSellOff = (TextView) convertView.findViewById(R.id.tvSellOff);//已售完

                    viewHolder.ivAddCombinationGoods.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("========", "onClick: 添加数据库");
                            GoodsEntity entity = list.get(position);
                            DBUtils.addShoppingCart(entity.getProductId(),entity.getGoodsName(),marketName,entity.getPrice(),entity.getIvImageUrl(),entity.getStock());
                            setBuyNumber(viewHolder,DBUtils.getShoppingCart(entity.getProductId()).getCount(),entity.getStock());
                        }
                    });
                    break;
                case 2://type为2时需要找到的控件，商品控件
                    convertView = layoutInflater.inflate(R.layout.other_goods_single_layout, null);
                    viewHolder.type = 2;//设置控件类型
                    viewHolder.ivGoodsImage = (ImageView) convertView.findViewById(R.id.ivGoodsImage);//商品图片
                    viewHolder.tvGoodsName = (TextView) convertView.findViewById(R.id.tvGoodsName);//商品名称
                    viewHolder.tvGoodsSellNo = (TextView) convertView.findViewById(R.id.tvGoodsSellNo);//销量
                    viewHolder.tvGoodsSinglePrice = (TextView) convertView.findViewById(R.id.tvGoodsSinglePrice);//商品价格

                    viewHolder.ivReduceCombinationGoods = (ImageView) convertView.findViewById(R.id.ivReduceCombinationGoods);
                    viewHolder.tvCombinationGoodsNumber = (TextView) convertView.findViewById(R.id.tvCombinationGoodsNumber);
                    viewHolder.ivAddCombinationGoods = (ImageView) convertView.findViewById(R.id.ivAddCombinationGoods);
                    viewHolder.tvSellOff = (TextView) convertView.findViewById(R.id.tvSellOff);

                    viewHolder.ivAddCombinationGoods.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("========", "onClick: 添加数据库");
                            GoodsEntity entity = list.get(position);
                            DBUtils.addShoppingCart(entity.getProductId(),entity.getGoodsName(),marketName,entity.getPrice(),entity.getIvImageUrl(),entity.getStock());
                            if(DBUtils.getShoppingCart(entity.getProductId())!=null)setBuyNumber(viewHolder, DBUtils.getShoppingCart(entity.getProductId()).getCount(), entity.getStock());}
                    });
                    break;
            }
            assert convertView != null;
            convertView.setTag(viewHolder);
            setDataToView(position, convertView);
        }

        return convertView;
    }

    /**
     * 设置数据到控件
     *
     * @param position    点击的位置
     * @param convertView 加载的converView
     */
    private void setDataToView(int position, View convertView) {
        ViewHolder viewHolder = (ViewHolder) convertView.getTag();
        GoodsEntity entity = getItem(position);
        switch (entity.getType()) {
            case 0://设置标题
                //标题设置在goodsName里面
                viewHolder.tvSchoolAreaName.setText(getItem(position).getGoodsName());
                break;
            case 1://设置组合特卖
                //设置图片2
                DownloadUtils.LoadPicture(entity.getIvOtherImageUrl(), viewHolder.ivCombinationGoods2);

                //设置商品2名称
                viewHolder.tvCombinationGoods2.setText(entity.getTvOtherGoodsName());

                //设置图片1
                DownloadUtils.LoadPicture(entity.getIvImageUrl(), viewHolder.ivCombinationGoods1);
                //设置商品1名称
                viewHolder.tvCombinationGoods1.setText(entity.getGoodsName());

                //设置总价格
                viewHolder.tvCombinationPrice.setText("¥" + entity.getPrice());

                //设置点击+的点击事件
                //  viewHolder.ivAddCombinationGoods.setOnClickListener(this);

                //设置点击-的点击事件
                // viewHolder.ivReduceCombinationGoods.setOnClickListener(this);

                //设置购买的状态和库存的状态
                ShoppingCart shoppingCart = DBUtils.getShoppingCart(entity.getProductId());
                if (shoppingCart != null) {
                    setBuyNumber(viewHolder, shoppingCart.getCount(), entity.getStock());
                } else {
                    setBuyNumber(viewHolder, 0, entity.getStock());
                }
                break;
            case 2://设置商品列表的数据
                //设置商品图片
                DownloadUtils.LoadPicture(entity.getIvImageUrl(), viewHolder.ivGoodsImage);

                //设置商品名称
                viewHolder.tvGoodsName.setText(entity.getGoodsName());

                //设置销量
                viewHolder.tvGoodsSellNo.setText("月销量" + entity.getSalesVolume());

                //设置价格
                viewHolder.tvGoodsSinglePrice.setText("¥" + entity.getPrice());

                //设置库存状态和购买数量
                ShoppingCart shoppingCart2 = DBUtils.getShoppingCart(entity.getProductId());
                if (shoppingCart2 != null) {
                    setBuyNumber(viewHolder, shoppingCart2.getCount(), entity.getStock());
                } else {
                    setBuyNumber(viewHolder, 0, entity.getStock());
                }
                break;
        }
    }

    /**
     * 设置购买状态和库存状态的函数
     *
     * @param viewHolder ViewHolder对象
     * @param buyNumber  购买的数量
     * @param stock      库存的数量
     */
    private void setBuyNumber(ViewHolder viewHolder, int buyNumber, int stock) {
        viewHolder.tvSellOff.setVisibility(View.GONE);
        viewHolder.ivAddCombinationGoods.setVisibility(View.GONE);
        viewHolder.ivReduceCombinationGoods.setVisibility(View.GONE);
        viewHolder.tvCombinationGoodsNumber.setVisibility(View.GONE);
        if (stock == 0) {//没有库存，显示已售完
            viewHolder.tvSellOff.setVisibility(View.VISIBLE);
        } else if (buyNumber == 0) {//没有加入购物车
            viewHolder.ivAddCombinationGoods.setVisibility(View.VISIBLE);
            viewHolder.ivAddCombinationGoods.setImageResource(R.drawable.add2);
        } else {//加入购物车
            viewHolder.ivAddCombinationGoods.setVisibility(View.VISIBLE);
            viewHolder.ivAddCombinationGoods.setImageResource(R.drawable.add1);
            viewHolder.tvCombinationGoodsNumber.setVisibility(View.VISIBLE);
            viewHolder.tvCombinationGoodsNumber.setText(buyNumber);
            viewHolder.ivReduceCombinationGoods.setVisibility(View.VISIBLE);
        }
    }

    protected class ViewHolder {

        private int type;//0代表 标题 1代表组合特卖 2代表商品列表
        //当type为0时设置goodsName属性为标题

        //type 为0时需要设置的布局
        private TextView tvSchoolAreaName;

        //type 为1时需要设置的布局
        private ImageView ivCombinationGoods1;
        private ImageView ivCombinationGoods2;
        private TextView tvCombinationPrice;
        private TextView tvCombinationGoods1;
        private TextView tvCombinationGoods2;

        //type 为2时需要设置的布局
        private ImageView ivGoodsImage;
        private TextView tvGoodsName;
        private TextView tvGoodsSellNo;
        private TextView tvGoodsSinglePrice;
        private ImageView ivReduceCombinationGoods;
        private TextView tvCombinationGoodsNumber;
        private ImageView ivAddCombinationGoods;
        private TextView tvSellOff;
    }
}
